#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
using namespace std;

//这里的count全部线程共享
//int count = 1;

//这里的count叫做局部存储
__thread int count = 1;
//线程局部存储有什么用？
//全局变量，我又不想让这个全局变量被其他线程看到！
//线程局部存储，只能存储内置类型和部分指针

string Addr(int& c)
{
    char addr[64];
    snprintf(addr, sizeof(addr), "%p", &c);
    return addr;
}

void* routine1(void* args)
{
    (void)args;
    while(true)
    {
        cout << "thread - 1, count: " << count << "修改count " << "&count:" << Addr(count) << endl;
        count++;
        sleep(1);
    }
}

void* routine2(void* args)
{
    (void)args;
    while(true)
    {
        cout << "thread - 2, count: " << count  << "&count:" << Addr(count) << endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid1,tid2;
    pthread_create(&tid1, nullptr, routine1, nullptr);
    pthread_create(&tid2, nullptr, routine2, nullptr);

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    return 0;
}